//
//  ProfileSettingViewController.m
//  aigo
//
//  Created by Phan Ba Minh on 12/30/12.
//  Copyright (c) 2012 AIGO. All rights reserved.
//

#import "ProfileSettingViewController.h"
#import "AppViewController.h"

@interface ProfileSettingViewController ()

@end

@implementation ProfileSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _APIRequester = [APIRequester new];
    
    // Interface
    _imgViewAvatar.layer.cornerRadius = INT_RADIUS_SMALL_AVATAR;
    _imgViewAvatar.layer.masksToBounds = YES;
    [_imgViewAvatar.layer setBorderWidth:INT_WIDTH_BORDER_SMALL_AVATAR];
    [_imgViewAvatar.layer setBorderColor:[UIColor whiteColor].CGColor];
    
    // Shadow
    UIImageView *ivShadow = [[UIImageView alloc] initWithFrame:_imgViewAvatar.frame];
    ivShadow.layer.masksToBounds = YES;
    ivShadow.layer.cornerRadius = INT_RADIUS_SMALL_AVATAR;
    ivShadow.layer.shadowColor = [UIColor blackColor].CGColor;
    ivShadow.layer.shadowOffset = CGSizeMake(INT_WIDTH_BORDER_SMALL_AVATAR*0, INT_WIDTH_BORDER_SMALL_AVATAR*9);
    ivShadow.layer.shadowOpacity = INT_WIDTH_BORDER_SMALL_AVATAR*0.7;
    ivShadow.layer.shadowRadius = INT_RADIUS_SMALL_AVATAR;
    ivShadow.backgroundColor = [UIColor whiteColor];
    ivShadow.clipsToBounds = NO;
    [self.view addSubview:ivShadow];
    [self.view bringSubviewToFront:_imgViewAvatar];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - APIRequesterProtocol

- (void)requestAPIWithType:(ENUM_API_REQUEST_TYPE)type {
    if (type == ENUM_API_REQUEST_TYPE_USER_LOGOUT) {
        NSMutableDictionary  *params = [NSMutableDictionary new];
        
        // TrongV 2012/11/21
        [params setObject:[AppViewController Shared].configDataModel.deviceToken forKey:STRING_REQUEST_KEY_DEVICE_TOKEN];
        
        [[AppViewController Shared] isRequesting:YES andRequestType:type andFrame:FRAME(0, 0, [SupportFunction getDeviceWidth], [SupportFunction getDeviceHeight] - HEIGHT_STATUS_BAR)];
        
        NSString *strURL = [NSString stringWithFormat:@"%@/%@", STRING_REQUEST_URL_USER_LOGOUT, [UserDataManager Shared].userID];
        
        [_APIRequester requestWithType:type andRootURL:strURL andPostMethodKind:YES andParams:params andDelegate:self];
    }
}

- (void)requestFinished:(ASIHTTPRequest *)request andType:(ENUM_API_REQUEST_TYPE)type {
    NSLog(@"requestFinished %@, request.responseStatusCode: %i", request.responseString, request.responseStatusCode);
    
    [[AppViewController Shared] isRequesting:NO andRequestType:type andFrame:CGRectZero];
    
    NSError *error;
    SBJSON *sbJSON = [SBJSON new];
    
    if (![sbJSON objectWithString:[request responseString] error:&error] || request.responseStatusCode != 200 || !request) {
//        if (![ASIHTTPRequest isNetworkReachable]) {
//            ALERT(STRING_ALERT_CONNECTION_ERROR_TITLE, STRING_ALERT_SERVER_ERROR);
//        }
        ALERT(STRING_ALERT_CONNECTION_ERROR_TITLE, [[sbJSON objectWithString:[request responseString] error:&error] objectForKey:STRING_RESPONSE_KEY_MSG]);
        return;
    }
    
    if (type == ENUM_API_REQUEST_TYPE_USER_LOGOUT) {
        [UserDataManager Shared].sessionID = @"";
        [UserDataManager Shared].loginStatus = NO;
        [[UserDataManager Shared] save];
        [[AppViewController Shared] changeBackToWelcome];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request andType:(ENUM_API_REQUEST_TYPE)type {
    NSLog(@" requestFailed %@ ", request.responseString);
    
    [[AppViewController Shared] isRequesting:NO andRequestType:type andFrame:CGRectZero];
    
    if (![ASIHTTPRequest isNetworkReachable]) {
        ALERT(STRING_ALERT_CONNECTION_ERROR_TITLE, STRING_ALERT_CONNECTION_ERROR);
    }
}

#pragma mark - Actions

- (IBAction)touchUpInsideLogout:(id)sender {
    [self requestAPIWithType:ENUM_API_REQUEST_TYPE_USER_LOGOUT];
}

- (IBAction)touchUpInsideBack:(id)sender {
    [[AppViewController Shared] changeBackFromProfileSetting];
}

- (IBAction)touchUpInsideDone:(id)sender {
    [[AppViewController Shared] changeBackFromProfileSetting];
}

- (void)viewDidUnload {
    [self setTableViewPersonInfo:nil];
    [self setTableViewBillingInfo:nil];
    [self setImgViewAvatar:nil];
    [super viewDidUnload];
}

#pragma mark UITableView Delegate & DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableViewPersonInfo) {
        return 3;
    }
    else {
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableViewPersonInfo) {
        static NSString *cellID = @"cellPersonInfoID";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        }
        
        if (indexPath.row == 0) {
            cell.textLabel.text = @"Name";
            cell.textLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.text = [UserDataManager Shared].userFullName;
            cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
            cell.detailTextLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
        }
        else if (indexPath.row == 1) {
            cell.textLabel.text = @"Email Adress";
            cell.textLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.text = [UserDataManager Shared].userEmail;
            cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
            cell.detailTextLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
        }
        else if (indexPath.row == 2) {
            cell.textLabel.text = @"Phone Number";
            cell.textLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.text = @"+1(408)-931-1846";
            cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
            cell.detailTextLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
        }
        
        cell.backgroundColor = RGBCOLOR(115, 116, 128);
        return cell;
    }
    else {
        static NSString *cellID = @"cellBillingInfoID";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        }
        
        if (indexPath.row == 0) {
            cell.textLabel.text = @"Account Balance";
            cell.detailTextLabel.text = @"398.00 VND";
            cell.textLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
        }
        else if (indexPath.row == 1) {
            cell.textLabel.text = @"Default Card Detail";
            cell.detailTextLabel.text = @"****000798";
            cell.textLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
        }
        else if (indexPath.row == 2) {
            cell.textLabel.text = @"(+) New Credit Card";
            cell.detailTextLabel.text = @"";
            cell.textLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
        }
        else if (indexPath.row == 3) {
            cell.textLabel.text = @"Tip Options";
            cell.detailTextLabel.text = @"";
            cell.textLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:FLOAT_DEFAULT_FONT_SIZE];
            cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
        }
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.backgroundColor = RGBCOLOR(115, 116, 128);
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

@end
